<template>
  <div class="edu-page-warp">
    <el-card shadow="never" body-style="padding: 24px;" class="card-margin">
      <h1 class="edu-page-title" v-text="$t('awardDegree.awardDegreeStandardLabel')" style="margin-bottom: 24px;"></h1>
      <el-form :model="pointForm" ref="degreeAwardedForm" label-width="100px">
        <el-form-item :label="$t('awardDegree.gradePointStandard') + ''" :rules="rules" prop="point">
          <el-input v-model="pointForm.point" style="width:220px">
          </el-input>
          <span>{{$t("awardDegree.gradePointRequest")}}{{pointForm.pointDefaultValue}}</span>
          <!-- <span style="margin-left: 16px;" class="hsClass"><el-checkbox v-model="pointForm.isHsk">{{$t('awardDegree.hskLabel')}}</el-checkbox></span> -->
        </el-form-item>
        <el-form-item>
          <el-button @click="cancel">{{$t("message.cancel")}}</el-button>
          <el-button type="primary" @click="ok(pointForm.point)">{{$t("message.confirm")}}</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<script>
  import api from 'common/src/api/graduationManagement/graduationManage';
  let setId = null;
  let initPointValue = null;
  export default {
    name: 'degreeAwardedMainten',
    data() {
      return {
        pointForm: {
          point: '',
          pointDefaultValue: 2.1,
          isHsk: false
        },
        cacheHsk: false,
        rules: [{
          validator: (rule, value, callback) => {
            let reg = /[^0-9\.]/g;
            if (reg.test(value)) {
              callback(new Error(this.$t('awardDegree.pointNumValidate')));
            } else {
              if (value <= 2.1) {
                callback(new Error('不能小于等于2.1'));
              }else {
                callback();
              }
            }
          }
        }],
        isOverseas: '1'
      }
    },
    mounted() {
      setId = null;
      initPointValue = null;
      this.queryPoint();
    },
    methods: {
      ok(value) {
        if (value == '') {
          this.$message.error(this.$t('awardDegree.pointValidate'));
          return;
        }
        this.$refs.degreeAwardedForm.validate((valid) => {
          if (valid) {
            //let isHsk = this.pointForm.isHsk === true ? '1' : '0';
            api.setStudentPoint(setId, Number(value), this.isOverseas).then(res =>{
              if (res && res.code === 200) {
                this.$message({
                  message: this.$t('awardDegree.pointSuccessTip'),
                  type: 'success'
                }); 
              }
            }).finally(() => {
              this.queryPoint();
            })
          } else {
            return false;
          }
        });
      },
      cancel() {
        this.pointForm.point = initPointValue;
        this.pointForm.isHsk = this.cacheHsk;
      },
      queryPoint() {
        api.getStudentPoint(this.isOverseas).then(res => {
          ({
            id: setId,
            scorePoint: initPointValue,
            isHsk: this.cacheHsk
          } = res.data || {});
          this.cacheHsk = this.cacheHsk === '1';
          this.pointForm.point = initPointValue;
          this.pointForm.isHsk = this.cacheHsk;
        });
      }
    }
  };

</script>
<style lang="scss" scoped>
  ::v-deep .hsClass span.el-checkbox__label {
    color: #303133;
  }
</style>
